# set -x
# 需要传入环境变量
# WEBHOOK: 钉钉群webhook地址
# 

# 测试README文件内容
readme_file="https://atomgit.com/crontask/readme/raw/master/README.md"
readme_md5="e485f93ca4ea1324a5f266ee724c000b"
commit_url="https://atomgit.com/api/v4/projects/81644/repository/commits/8581fae5fd7ab4ef6c46991114ab7dd0e6ad0f35?_input_charset=utf-8&sha=11&show_signature=true"
http_clone_url="https://atomgit.com/crontask/readme.git"
ssh_clone_url="git@atomgit.com:crontask/readme.git"
http_repo_private_url="https://${GIT_HTTP_TOKEN}@atomgit.com/crontask/crontask-http.git"

# webhook 地址，从参数中获取
webhook_url=$WEBHOOK
echo "webhook: $webhook_url"

# 新建一个文件用于存储标准错误信息
log_file=/tmp/task.log

touch $log_file
# exec 6< /tmp/errors.log

function report() {
    log=`cat /tmp/${1}.log`
    echo "出现错误，错误码 ${1} 错误信息 ${2}, 日志 $log"
    curl -s ${webhook_url} \
    -H "Content-Type: application/json" \
    -d "{\"msgtype\": \"markdown\", \"at\": {\"atMobiles\": [\"13681741604\"]}, \"markdown\": {\"title\":\"巡检出错\", \"text\": \"## 巡检出错 \n **错误码**: ${1} \n\n **错误信息**: ${2} \n\n **错误日志**: ${log}\"}}" \
    -o /dev/null
}


# 1. 测试文件是否正常获取
curl ${readme_file} -o /tmp/readme_file >>/tmp/1.log 2>&1
if [ $? -ne 0 ]; then
    report 1 "获取文件失败 ${readme_file}"
    exit 1
fi

md5=`cat /tmp/readme_file | md5sum | awk '{print $1}'`

# 2. 比较 md5
if [ ${md5}x != ${readme_md5}x ]; then
    report 2 "文件内容不匹配：期望(${readme_md5}) != 实际(${md5})"
    exit 1
fi

# 3. 测试 commit 是否存在
commit_content=`curl -s ${commit_url}`
if [ $? -ne 0 ]; then
    report 3 "获取commit失败 ${commit_url}"
    exit 1
fi

# 4. 测试 commit 的内容
if echo "${commit_content}" | grep -q 'Initial commit'; then
echo "commit内容匹配"
else
  report 4 "commit内容不匹配 ${commit_url}"
  exit 1
fi

# 50. 测试匿名 http clone
git clone ${http_clone_url} /tmp/readme-http-$RANDOM.git >>/tmp/50.log 2>&1
if [ $? -ne 0 ]; then
    report 50 "http clone 失败"
    exit 1
fi

# 51. 测试私有仓库 http clone
private_repo=/tmp/crontask-http-$RANDOM.git
git clone ${http_repo_private_url} ${private_repo} >>/tmp/51.log 2>&1
if [ $? -ne 0 ]; then
    report 51 "http clone 私有仓库失败"
    exit 1
fi

# 52. 测试私有仓库 http push
git config --global user.name "crontask"
git config --global user.email "crontask@aliyun.com"

date > ${private_repo}/update && git -C ${private_repo} add -A && git -C ${private_repo} commit -m "crontask"
git -C ${private_repo} push origin HEAD:master >>/tmp/52.log 2>&1
if [ $? -ne 0 ]; then
    report 52 "http push 仓库失败"
    exit 1
fi

# 53. 测试私有仓库 agit-flow

# date > ${private_repo}/update && git -C ${private_repo} add -A && git -C ${private_repo} commit -m "crontask"
# git -C ${private_repo} push origin HEAD:refs/for/master >>/tmp/53.log 2>&1
# if [ $? -ne 0 ]; then
#     report 53 "创建评审失败"
#     exit 1
# fi

# 60. 测试 ssh key
# echo id_ed25519.pub:$SSH_PUBLIC_KEY
# echo -e id_ed25519:$SSH_PRIVATE_KEY
mkdir -p /root/.ssh
echo $SSH_PUBLIC_KEY > /root/.ssh/id_ed25519.pub
echo -e $SSH_PRIVATE_KEY > /root/.ssh/id_ed25519
echo $SSH_KNOWN_HOSTS > /root/.ssh/known_hosts

chmod 600 /root/.ssh/id_ed25519
chmod 644 /root/.ssh/id_ed25519.pub
chmod 644 /root/.ssh/known_hosts

ssh -T git@atomgit.com >>/tmp/60.log 2>&1
if [ $? -ne 0 ]; then
    report 60 "ssh key 测试失败"
    exit 1
fi

# 61. 测试 ssh clone
git clone ${ssh_clone_url} /tmp/readme-ssh-$RANDOM.git >>/tmp/61.log 2>&1
if [ $? -ne 0 ]; then
    report 61 "ssh clone 测试失败"
    exit 1
fi

echo "测试正常"
exit 0